package com.ruoyi.ocean.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ocean.domain.bo.OceanLogBo;
import com.ruoyi.ocean.domain.vo.OceanLogVo;
import com.ruoyi.ocean.domain.OceanLog;
import com.ruoyi.ocean.mapper.OceanLogMapper;
import com.ruoyi.ocean.service.IOceanLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 操作日志Service业务层处理
 *
 * @author linziheng
 * @date 2024-05-08
 */
@RequiredArgsConstructor
@Service
public class OceanLogServiceImpl implements IOceanLogService {

    private final OceanLogMapper baseMapper;

    /**
     * 查询操作日志
     */
    @Override
    public OceanLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询操作日志列表
     */
    @Override
    public TableDataInfo<OceanLogVo> queryPageList(OceanLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OceanLog> lqw = buildQueryWrapper(bo);
        Page<OceanLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询操作日志列表
     */
    @Override
    public List<OceanLogVo> queryList(OceanLogBo bo) {
        LambdaQueryWrapper<OceanLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OceanLog> buildQueryWrapper(OceanLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OceanLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), OceanLog::getAccountId, bo.getAccountId());
        lqw.eq(StringUtils.isNotBlank(bo.getContentTitle()), OceanLog::getContentTitle, bo.getContentTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContentLog()), OceanLog::getContentLog, bo.getContentLog());
        lqw.eq(StringUtils.isNotBlank(bo.getObjectId()), OceanLog::getObjectId, bo.getObjectId());
        lqw.like(StringUtils.isNotBlank(bo.getObjectName()), OceanLog::getObjectName, bo.getObjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getObjectType()), OceanLog::getObjectType, bo.getObjectType());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), OceanLog::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getOptIp()), OceanLog::getOptIp, bo.getOptIp());
        lqw.eq(bo.getCreateTime() != null, OceanLog::getCreateTime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增操作日志
     */
    @Override
    public Boolean insertByBo(OceanLogBo bo) {
        OceanLog add = BeanUtil.toBean(bo, OceanLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改操作日志
     */
    @Override
    public Boolean updateByBo(OceanLogBo bo) {
        OceanLog update = BeanUtil.toBean(bo, OceanLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OceanLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除操作日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
